<?php
class Salary extends Public_Controller{
	public $profiles;
	public $cache_profile = 'emp_profile/profile_';
	function __construct(){
		parent::__construct();
		$this->load->model('profile_m');
		$this->load->library('kunkuncache');
		/*
		khi chạy vào đây sẽ lưu cache toàn bộ thông tin nhân viên vào cache -> có nên hay ko ? 
		Lưu từng file hay lưu 1 file ? 
		*/
		$this->load->helper('profile');
		$this->profiles = list_profile();
		$this->template->set_layout('salary.html')->append_css('breadcrumb.css');
		menu_active($this->controller);
		$this->load->model('salary_m');
		if($this->role_full || $this->current_user->group=='boss2'){
			$this->session->set_userdata('redirect', $this->uri->uri_string());			
			$this->template->set('admin',true)->append_css('toolbar.css');
			$this->action();
		}
	}
	private function action($room_id = NULL){
		$this->action  = 'excel|'.site_url('emp_profile/import_salary').'|'.lang('salary:viewfile');
		$this->action .='+ excel|'.site_url('emp_profile/export_salary').'|'.lang('global:export_file');
		if($this->current_user->group_id == 1)
		$this->action .= '+ excel|'.site_url('emp_profile/import_salary/import_deduction').'|'.lang('deduction:import_deduction');
		if(!empty($room_id)){
		$this->action .= //'+ new|'.site_url('emp_profile/salary/plus/'.$room_id).'|'.lang('salary:plus').
						 //'+ remove|'.site_url('emp_profile/salary/minus/'.$room_id).'|'.lang('salary:minus').
						'+ factor|'.site_url('emp_profile/salary/factor/'.$room_id).'|'.lang('salary:factor').
						'+ allows|'.site_url('emp_profile/salary/allows/'.$room_id).'|'.lang('salary:allows');
		}
		$this->template->set('action',$this->action);
	}
	private function room_data($room_id = NULL){
		$this->load->model('emp_room/room_m');
		$room = !empty($room_id) ? $this->room_m->get($room_id) : NULL;
		return $room;
	}

	private function employees_select($room_id){
		$childs = $this->room_m->get_all($parent_id = $room_id);
		$room_id_childs = array();
		foreach($childs as $i){
			$room_id_childs[] = $i->id;
		}
		$tmp = array();
		$this->room_m->array_room_child($room_id,$tmp);
		$list_id_room_childs = join(',',$tmp); // 4,2,1
		$employees = $this->profile_m->get_by_room($room_id,$list_id_room_childs);
		$employees_select = array_for_select($employees,'id','display_name');
		$this->template->set('employees_select',$employees_select);
		$list_room = array_merge(array($room_id),$tmp);
		return $list_room;
		
	}
	private function append_base_where($room_id,&$base_where){ // update base_where khi co room_id
		$base_where['user_id'] = $this->input->get('user_id');
		$base_where['room_id'] = $room_id;
		$list_room = $this->employees_select($room_id);
		$base_where['list_room'] = $list_room;
		$this->action($room_id);
	}
	private function append_metadata(){
		$this->template
			->append_css('salary.css')
			->append_css('map.css')
			->append_js('ui/jquery-ui.min.js')
			->append_css('ui/jquery-ui.min.css')
			->append_js('admin/plugins.js')
			->append_js('admin/scripts.js')
			->append_js('business/site.js')
			->append_js('chosen/chosen.jquery.min.js')
			->append_css('chosen/chosen.css')
			->append_js('filter.js');
	}
	function index($room_id = NULL){	
		$base_where = array('user_id'=>$this->current_user->id);
		$room = $this->room_data($room_id);
		$rooms = $this->room_m->get_all_list($parent_id = 0 ,$data = NULL,$tag = NULL,$link = "emp_profile/salary/");
		$tit = !empty($room_id) ? $room->title : "";
		//lay nhan vien cua phong ban het het het con con con
		if(!empty($room_id)){
			$this->append_base_where($room_id,$base_where);
		}
		$this->load->helper('admin_theme');
		$base_where = $this->input->get('year') ? $base_where + array('year' => $this->input->get('year')) : $base_where;
		$base_where = $this->input->get('month') ? $base_where + array('month' => $this->input->get('month')) : $base_where;
		
		$this->load->model('number_m');
		$year_select = $this->number_m->year_select($this->year_start,date("Y"));
		$month_select = $this->number_m->month_select();
		
		$model = $this->salary_m->get_many_by($base_where);
		
		$this->template
			->title(lang('salary:salary'));
			if($this->current_user->group_id == 1){
				$this->template
				->set_breadcrumb(lang('salary:salary_room') . " " . $tit);
			}
			else{
				$this->template
				->set_breadcrumb(lang('salary:single'));
			}
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$year = $this->input->get('year') ? $this->input->get('year') : NULL;
		$this->template
			->set(array(
				'year_select'=>$year_select,
				'month_select' => $month_select,
				'model'	=> $model,
				'year'	=> $year,
				'rooms' => $rooms,
				'loading' => true
			));
		$this->append_metadata();
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/list') : $this->template->build($this->controller.'/index');
	}
	
	//protected plus - minus
	private function plus_minus($room_id,$method, $filter = true){
		$room = $this->room_data($room_id);
		$rooms = $this->room_m->get_all_list($parent_id = 0 ,$data = NULL,$tag = NULL,$link = "emp_profile/salary/$method/");
		$tit = !empty($room_id) ? $room->title : "";
		if(!empty($room_id)){
			$this->append_base_where($room_id,$base_where);
		}
		$this->load->helper('admin_theme');
		$this->load->helper('admin_theme');
		$base_where = $this->input->get('year') ? $base_where + array('year' => $this->input->get('year')) : $base_where;
		$base_where = $this->input->get('month') ? $base_where + array('month' => $this->input->get('month')) : $base_where;
		$month = $this->input->get('month') ?  $this->input->get('month') : "";
		$this->load->model('number_m');
		$year_select = $this->number_m->year_select($this->year_start,date("Y"));
		$month_select = $this->number_m->month_select();
		
		$model = $this->salary_m->get_many_by($base_where);
		$this->template
			->title(lang("salary:$method"))
			->set_breadcrumb(lang("salary:$method") . " " . $tit);
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$year = $this->input->get('year') ? $this->input->get('year') : NULL;
		$this->template
			->set(array(
				'year_select'=>$year_select,
				'month_select' => $month_select,
				'model'	=> $model,
				'year'	=> $year,
				'rooms' => $rooms,
				'month'	=> $month,
				'title'	=> lang("salary:$method") . " " . $tit . " - ",
				'method'=>$method,
				'filter' => $filter
			))->append_css('form.table.css');
		$this->append_metadata();
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/plus_minus') : $this->template->build($this->controller.'/plus_minus');
	}
	//cac khoan cong 
	public function plus($room_id){
		$this->plus_minus($room_id,$method = $this->method);
	}
	//cac khoan tru
	public function minus($room_id){
		$this->plus_minus($room_id,$method = $this->method);
	}
	public function factor($room_id){
		$this->plus_minus($room_id,$method = $this->method);
	}
	//ngay phep
	public function allows($room_id){
		if($this->current_user->group_id != 1)
			redirect();
		$this->plus_minus($room_id,$method = $this->method,$filter = false);
	}
	function update_profile(){
		$users = array();
		$model = $this->input->post();//users . allows_1
		foreach($model['allows_1'] as $u=>$allows_1){
			$update = array('allows_1'=>$allows_1);
			$this->profile_m->update($u,$update);
		}
		//xoa cache
		refresh_profile();
	}
	//update emp_salary_2013 plus, plus_note, minus , minus_note
	public function update_plus_minus($method,$year,$month){
		$data = $this->input->post();
		$this->salary_m->update_plus_minus($method,$year,$month,$data);
	}
}